package com.ting.template.core.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GeneraterUtil {
	private static final Logger logger = LoggerFactory.getLogger(GeneraterUtil.class);
	private static final String DEFAULT_DATE_FORMAT = "yyyyMMddHHmmss";

	/**
	 * Get a sequence by key , prefix by currentDate , and length like
	 * yymmdd##########
	 */
	public static String generateSeqByCurrDate(final String key, final int length) {
		String result = getFixValueByDate(key, length);
		logger.info("GeneraterUtil|generateSqByCurrDate|key: {},length: {}, result: {}", key, length, result);
		return result;
	}

	/**
	 * Get a sequence by key , prefixStr , and length like A##########
	 */
	public static String generateSqByPrefix(final String key, final String prefixStr, final int length) {
		String result = getFixValueByPrefix(prefixStr, key, length);
		logger.info("GeneraterUtil|generateSqByPrefix|key: {}, prefixStr: {}, length: {}, result: {}", key, prefixStr,
				length, result);
		return result;
	}

	/**
	 * getValueByFixedLength
	 */
	public static String getValueByFixedLength(String key, int length) {
		int len = key.length();
		if (length <= len) {
			return key.substring(len - length);
		}
		String tmp = key;
		for (int i = 0; i < (length - len); i++) {
			Random r = new Random();
			tmp += r.nextInt(9);
		}
		return tmp;
	}

	/**
	 * getFixValueByPrefix
	 */
	public static String getFixValueByPrefix(String prefix, String key, int length) {
		StringBuilder result = new StringBuilder(prefix);
		result.append(getValueByFixedLength(key, length));
		return result.toString();
	}

	/**
	 * getFixValueByDate
	 */
	public static String getFixValueByDate(String key, int length) {
		StringBuilder result = new StringBuilder("");
		result.append((new SimpleDateFormat(DEFAULT_DATE_FORMAT)).format(new Date()));
		result.append(getValueByFixedLength(key, length));
		return result.toString();
	}

	// 生成代理商ID
	public static String getrevenueId(String mobile) {
		StringBuilder result = new StringBuilder("6");
		for (int i = 0; i < 4; i++) {
			Random r = new Random();
			result.append(r.nextInt(9));
		}
		result.append(mobile.substring(mobile.length() - 4));
		return result.toString();
	}
	
	// 生成商户id:8+6位随机数+手机号后4位
	public static String generateMerchantId(String mobile) {
		StringBuilder result = new StringBuilder("8");
		for (int i = 0; i < 6; i++) {
			Random r = new Random();
			result.append(r.nextInt(9));
		}
		result.append(mobile.substring(mobile.length() - 4));
		return result.toString();
	}

	/**
	 * 获取token
	 * @return
	 */
	public static String getToken(){
		StringBuilder result = new StringBuilder("");
        String s = UUID.randomUUID().toString();
        String ss= s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)	+ s.substring(19, 23) + s.substring(24);
        result.append(ss).append((new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()));
        return  result.toString().substring(3, 45);
	}
	/**
	 * 获取二维码id 9位随机数
	 * @return
	 */
	public static String getQrCodeId(){
		StringBuilder result = new StringBuilder("");
        result.append(getRandomString(5)).append((new SimpleDateFormat("HHmmss")).format(new Date())).append(getRandomString(3));
        return result.toString();
	}
	//随机洗牌算法
	public static String getRandomString(int len){
		String no="";  
        int num[]=new int[len];  
        int c=0;  
        for (int i = 0; i < len; i++) {  
            num[i] = new Random().nextInt(10);  
            c = num[i];  
            for (int j = 0; j < i; j++) {  
                if (num[j] == c) {  
                    i--;  
                    break;  
                }  
            }  
        }  
        if (num.length>0) {  
            for (int i = 0; i < num.length; i++) {  
                no+=num[i];  
            }  
        }  
        return no; 
	}
	

	/**
	 * 生成带随机数的签约报备请求号
	 * @param merchantId
	 * @return
	 */
	public static String getAccIdByRandom(String merchantId,String prefix,int len){
		StringBuilder result = new StringBuilder(prefix);
		result.append(merchantId);
		result.append((new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()));
		for (int i = 0; i < len; i++) {
			Random r = new Random();
			result.append(r.nextInt(9));
		}
		return result.toString();
	}
	
	/**
	 * 生成UUID随机数，作为发给短信服务的请求号
	 * @return
	 */
	public static String generateRequestIdByUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}


	/**
	 * getFixValueByDate
	 */
	public static String getDateyyyyMMddHHmmss() {
		return new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(new Date());
	}
	
}
